package com.xqkad.myplayer.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.xqkad.myplayer.R;
import com.xqkad.myplayer.activity.PlayingActivity;
import com.xqkad.myplayer.activity.ReceiverActivity;

import java.util.ArrayList;
import java.util.List;

public class TabFragment extends Fragment {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private String[] titles = new String[]{"科技美学","李小新"};

    public static TabFragment newInstance() {
        TabFragment fragment = new TabFragment();
        Bundle args = new Bundle();
        //args.putString(ARG_PARAM1, param1);
        //args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootview = inflater.inflate(R.layout.fragment_tab, container, false);
        tabLayout = rootview.findViewById(R.id.tabs);
        viewPager = rootview.findViewById(R.id.viewpager);
        setupViewPager();
        return rootview;
    }

    private void setupViewPager(){
        Adapter adapter = new Adapter(getChildFragmentManager());
        adapter.addFragment(MusicFragment.newInstance(titles[0]), titles[0]);
        adapter.addFragment(MusicFragment.newInstance(titles[1]), titles[1]);
        //adapter.addFragment(new ArtistFragment(), title[1]);
        //adapter.addFragment(new AlbumFragment(), title[2]);
        //adapter.addFragment(new FolderFragment(), title[3]);

        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    private static class Adapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            if(mFragments.size() > i)
                return mFragments.get(i);

            return null;
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }
    }
}
